from marshmallow import Schema, fields
from datetime import datetime
from applications.authz import current_user


class TokenSchema(Schema):
    user_id = fields.Integer()
    iat = fields.DateTime()
    exp = fields.DateTime()


def get_token():
    token_schema = TokenSchema()
    cu = {key: current_user[key] for key in ["user_id", "iat", "exp"]}
    cu["iat"] = datetime.fromtimestamp(cu["iat"])
    cu["exp"] = datetime.fromtimestamp(cu["exp"])
    return token_schema.dump(cu)


def get_user():
    token_schema = TokenSchema()
    cu = {key: current_user[key] for key in ["user_id", "iat", "exp"]}
    cu["iat"] = datetime.fromtimestamp(cu["iat"])
    cu["exp"] = datetime.fromtimestamp(cu["exp"])
    return token_schema.dump(cu)
